package demo.huodongbaoming.controller;

import demo.huodongbaoming.dto.ActivityDTO;
import demo.huodongbaoming.dto.ActivityRequest;
import demo.huodongbaoming.dto.ResponseResult;
import demo.huodongbaoming.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityNotFoundException;
import java.util.List;

@RestController
@RequestMapping("/api/activities")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @PostMapping
    public ResponseResult<ActivityDTO> createActivity(@RequestBody ActivityRequest activityRequest,
                                                      @RequestParam Long creatorId) {
        try {
            ActivityDTO activityDTO = activityService.createActivity(activityRequest, creatorId);
            return ResponseResult.success("活动创建成功", activityDTO);
        } catch (RuntimeException e) {
            return ResponseResult.fail(400, e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResponseResult<ActivityDTO> getActivityById(@PathVariable Long id) {
        try {
            ActivityDTO activityDTO = activityService.getActivityById(id);
            return ResponseResult.success(activityDTO);
        } catch (EntityNotFoundException e) {
            return ResponseResult.fail(404, e.getMessage());
        }
    }

    @GetMapping
    public ResponseResult<List<ActivityDTO>> getAllActivities() {
        List<ActivityDTO> activities = activityService.getAllActivities();
        return ResponseResult.success(activities);
    }

    @GetMapping("/active")
    public ResponseResult<List<ActivityDTO>> getActiveActivities() {
        List<ActivityDTO> activities = activityService.getActiveActivities();
        return ResponseResult.success(activities);
    }

    @GetMapping("/creator/{creatorId}")
    public ResponseResult<List<ActivityDTO>> getActivitiesByCreator(@PathVariable Long creatorId) {
        try {
            List<ActivityDTO> activities = activityService.getActivitiesByCreator(creatorId);
            return ResponseResult.success(activities);
        } catch (EntityNotFoundException e) {
            return ResponseResult.fail(404, e.getMessage());
        }
    }

    @GetMapping("/category/{categoryId}")
    public ResponseResult<List<ActivityDTO>> getActivitiesByCategory(@PathVariable Long categoryId) {
        try {
            List<ActivityDTO> activities = activityService.getActivitiesByCategory(categoryId);
            return ResponseResult.success(activities);
        } catch (EntityNotFoundException e) {
            return ResponseResult.fail(404, e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ResponseResult<ActivityDTO> updateActivity(@PathVariable Long id,
                                                      @RequestBody ActivityRequest activityRequest) {
        try {
            ActivityDTO activityDTO = activityService.updateActivity(id, activityRequest);
            return ResponseResult.success("活动更新成功", activityDTO);
        } catch (EntityNotFoundException e) {
            return ResponseResult.fail(404, e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteActivity(@PathVariable Long id) {
        try {
            activityService.deleteActivity(id);
            return ResponseResult.success("活动删除成功", null);
        } catch (EntityNotFoundException e) {
            return ResponseResult.fail(404, e.getMessage());
        }
    }

    @PostMapping("/{activityId}/register/{userId}")
    public ResponseResult<ActivityDTO> registerForActivity(@PathVariable Long activityId,
                                                          @PathVariable Long userId) {
        try {
            ActivityDTO activityDTO = activityService.registerForActivity(activityId, userId);
            return ResponseResult.success("报名成功", activityDTO);
        } catch (RuntimeException e) {
            return ResponseResult.fail(400, e.getMessage());
        }
    }

    @DeleteMapping("/{activityId}/register/{userId}")
    public ResponseResult<ActivityDTO> cancelRegistration(@PathVariable Long activityId,
                                                         @PathVariable Long userId) {
        try {
            ActivityDTO activityDTO = activityService.cancelRegistration(activityId, userId);
            return ResponseResult.success("取消报名成功", activityDTO);
        } catch (RuntimeException e) {
            return ResponseResult.fail(400, e.getMessage());
        }
    }

    @GetMapping("/registered/{userId}")
    public ResponseResult<List<ActivityDTO>> getRegisteredActivities(@PathVariable Long userId) {
        try {
            List<ActivityDTO> activities = activityService.getRegisteredActivities(userId);
            return ResponseResult.success(activities);
        } catch (EntityNotFoundException e) {
            return ResponseResult.fail(404, e.getMessage());
        }
    }
} 